package com.tongzhan.controller;

import com.tongzhan.base.BaseController;
import com.tongzhan.base.Return;
import com.tongzhan.entity.qiaotai.OverseasIntroduce;
import com.tongzhan.param.IntroduceParam;
import com.tongzhan.utils.SnowFlakeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 组织建设-侨台简介
 * @author htmic
 * @date 2020/7/31
 */
@RequestMapping("/org")
@RestController
public class OrgIntroduceController extends BaseController {

    /**
     * 列表
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/qiaotaiIntroduceList")
    public Return personnelList(Integer page, Integer size) {
        page = page == null ? 0 : page - 1;
        Page<OverseasIntroduce> list = orgIntroduceService.getList(page, size);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list.getContent());
        map.put("total", list.getTotalElements());
        map.put("totalpage", list.getTotalPages());
        return Return.getSuccessResult(map);
    }

    /**
     * 添加
     * @param param
     * @return
     */
    @PostMapping("/addQiaotaiIntroduce")
    public Return add(@RequestBody IntroduceParam param) {
        if (param == null || param.getName() == null
                ||param.getType()==null||param.getPicture()==null) {
            return Return.getErrorResult("参数为空");
        }
        if (param.getName().length() > 100) {
            return Return.getErrorResult("名字过长");
        }

        OverseasIntroduce entity = new OverseasIntroduce();
        BeanUtils.copyProperties(param, entity);
        entity.setId(new SnowFlakeUtils(3, 3).nextId()+"");
        entity.setAddUserId(param.getUserId());
        long l = orgIntroduceService.add(entity);
        if (l > 0) {
            return Return.getSuccessResult("新增成功!");
        }
        return Return.getErrorResult("新增失败!");
    }


    /**
     * 详情
     * @param id
     * @return
     */
    @PostMapping("/qiaotaiIntroduceDetail")
    public Return getDetail(String id) {
        if (id == null) {
            return Return.getErrorResult("缺少id参数!");
        }
        OverseasIntroduce vo = orgIntroduceService.getDetail(id);
        return Return.getSuccessResult(vo);
    }
}
